package com.bdqn.controller;


import com.bdqn.entity.Orders;
import com.bdqn.param.requset.OrderParam;
import com.bdqn.param.requset.Status;
import com.bdqn.param.response.DishDto;
import com.bdqn.param.response.OrdersDto;
import com.bdqn.param.response.Page;
import com.bdqn.service.OrdersService;
import com.bdqn.service.ShoppingCartService;
import com.bdqn.util.RespBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author hunter
 * @since 2022-06-11
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    //全局变量的话 会默认初始化  局部变量的话 我们则必须初始化
    int sumNum;
    /**
     * 服务端的订单展示
     * @param page
     * @param pageSize
     * @param orderParam
     * @return
     */
    @GetMapping("/page")
    public RespBean page(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize, OrderParam orderParam) {
        Page<Orders> ordersPage = ordersService.pageInfo(page, pageSize, orderParam);
        return ordersPage != null ? RespBean.ok("获取分类管理列表成功", ordersPage) : RespBean.error("获取分类管理列表失败");
    }

    /**
     * 根据传来的订单的id和状态码  那么我们进行修改订单的进度状态
     * @param status
     * @return RespBean
     */
    @PutMapping
    public RespBean update(@RequestBody Status status){
        try {
            Orders orders = new Orders();
            orders.setId(status.getId());
            orders.setStatus(status.getStatus());
           ordersService.updateById(orders);
            return RespBean.error("更改状态成功");
        }catch (Exception e){
            return RespBean.error("更改状态失败");
        }
    }

    /**
     * 客户端的订单展示
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public RespBean userPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer pageSize, HttpServletRequest request){
        System.out.println("获取的路径为"+request.getRequestURI());
       log.info("获取的请求路径为: {}",request.getRequestURI());
        Page<OrdersDto> ordersPage = ordersService.userPageInfo(page, pageSize,sumNum);
        return ordersPage != null ? RespBean.ok("获取分类管理列表成功", ordersPage) : RespBean.error("获取分类管理列表失败");
    }
    /**
     * 提交订单的请求  接收用户的备注  订单表增加一条订单信息 及支付的方式 (1微信,2支付宝)
     * 步骤:
     *      ① 添加一条主订单
     *      ② 添加主订单对应详细子订单  (绑定主订单id)
     *      ③ 清空购物车
     */
    @PostMapping("/submit")
    public RespBean submit(@RequestBody  Orders orders, HttpSession session){
       //逻辑在service完成
        sumNum = ordersService.addOrder(orders, shoppingCartService.list(), session);
        return sumNum>0?RespBean.ok("添加订单成功",sumNum):RespBean.error("添加订单失败");
    }
}
